// pages/mine/mine.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user:'',
    essaylist:[],
    flag:'',
    headimg:'',
    show: false,
    open:false,
    actions: [
      {
        name: '拍摄',
        subname: '照片或视频',
      },
      {
        name: '从相册选择'
      }
    ]
  },
  onClose() {
    this.setData({ show: false });
  },
  close(){
    this.setData({open:false})
  },
  onSelect(event) {
    console.log(event.detail);
    if(event.detail.name=='从相册选择'){
        wx.chooseMedia({
            count:3,
            sizeType:['origin'],
            sourceType:['album'],
            success:res=>{
                console.log(res.tempFiles.length)
                console.log(res.tempFiles)
                let picarr=[]
                for (let i=0;i<res.tempFiles.length;i++){
                    picarr.push(res.tempFiles[i].tempFilePath)
                }
                picarr=JSON.stringify(picarr)
                wx.navigateTo({
                  url: '/pages/a5/a5?pics='+picarr,
                })
            },
            fail:err=>{
                console.error(err)
            }
        })
    }
  },
  detail(){
    wx.navigateTo({
    url: '/pages/a1/a1?user='+this.data.user
    })
},
create(){
  this.setData({ show: true });
  
},
tap_drag(e){
  this.data.newmark = e.touches[0].pageX;
    // 手指从左向右移动
    if (this.data.mark < this.data.newmark) {
      this.istoright = true;
    }
    // 手指从右向左移动
    if (this.data.mark > this.data.newmark) {
      this.istoright = false;
    }
    this.data.mark = this.data.newmark;
},
tap_start(e){
  console.log(e)
  this.data.mark = this.data.newmark = e.touches[0].pageX;
},
tap_end(e){
  this.data.mark = 0;
    this.data.newmark = 0;
    // 通过改变 opne 的值，让主页加上滑动的样式
    if (this.istoright) {
      this.setData({
        open: true
      });
    } else {
      this.setData({
        open: false
      });
    }

},
open(){
  console.log('open')
  this.setData({open:true})
},
history(){
  wx.navigateTo({
    url: '/pages/map/map',
  })
},
check(){
  wx.navigateTo({
    url: '/pages/a7/a7',
  })
},
help(){
  wx.navigateTo({
    url: '/pages/a8/a8',
  })
},
center(){
  wx.navigateTo({
    url: '/pages/a9/a9',
  })
},
goattention(){
  wx.navigateTo({
    url: '/pages/interestme/interestme',
  })
},
gofans(){
  wx.navigateTo({
    url: '/pages/fans/fans',
  })
},
details(e){
  let id=e.currentTarget.id
  wx.navigateTo({
    url: '/pages/details/details?id='+id,
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({flag:getApp().flag});
    this.setData({user:getApp().globalData.user})
    // wx.request({
    //   url: 'http://localhost:3000/essay/byuser',
    //   method: 'GET',
    //   data:{
    //     userid:this.data.user.id
    //   },
    //   success:res=>{
    //     this.setData({essaylist:res.data})
    //     console.log(res.data)
    //     console.log(this.data)
    //   },
    //   fail:err=>{
    //     console.log(err)
    //   }
    // })
  },
  changehead(){
    wx.chooseMedia({
        count:1,
        sizeType:['origin'],
        sourceType:['album'],
        success:res=>{
          console.log(res)
          this.setData({url:res.tempFiles[0].tempFilePath})
          console.log(res.tempFiles[0].tempFilePath)
            wx.uploadFile({
              filePath: this.data.url,
              name: 'file',
              url: 'http://localhost:3000/user/upload',
              success:res=>{
                  let path='../../demo/upload/'+res.data
                  this.setData({'user.image':path})
                  console.log(res)
                  wx.request({
                    url: 'http://localhost:3000/user/changehead',
                    data:{
                        id:getApp().globalData.user.id,
                        path:path
                    },
                    success:res=>{
                        console.log(res)
                    },
                    fail:err=>{
                        console.error(err)
                    }
                  })
              },
              fail:err=>{
                  console.error(err)
              }
            })
        }
      })
  },
  changebg(){
    wx.chooseMedia({
        count:1,
        sizeType:['origin'],
        sourceType:['album'],
        success:res=>{
          console.log(res)
          this.setData({bgurl:res.tempFiles[0].tempFilePath})
          console.log(res.tempFiles[0].tempFilePath)
            wx.uploadFile({
              filePath: this.data.bgurl,
              name: 'file',
              url: 'http://localhost:3000/user/upload',
              success:res=>{
                  let path='../../demo/upload/'+res.data
                  this.setData({'user.background':path})
                  console.log(res)
                  wx.request({
                    url: 'http://localhost:3000/user/changebg',
                    data:{
                        id:getApp().globalData.user.id,
                        path:path
                    },
                    success:res=>{
                        console.log(res)
                    },
                    fail:err=>{
                        console.error(err)
                    }
                  })
              },
              fail:err=>{
                  console.error(err)
              }
            })
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({user:getApp().globalData.user})
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({flag:getApp().flag});
    this.setData({user:getApp().globalData.user})
    this.setData({show:false})
    this.setData({open:false})
    wx.request({
      url: 'http://localhost:3000/essay/byuser',
      method: 'GET',
      data:{
        userid:this.data.user.id
      },
      success:res=>{
        this.setData({essaylist:res.data})
        console.log(res.data)
        console.log(this.data)
      },
      fail:err=>{
        // 
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  login(){
    wx.navigateTo({
      url: '/pages/200/200',
    })
  }
})